package com.alliky.core.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alliky.core.R;
import com.alliky.core.config.Config;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/8/1 15:56
 * ================================================
 */
public class RxToast {

    public static void success() {
        success("发送成功", 0);
    }

    public static void success(String message) {
        success(message, 0);
    }

    public static void success(String message, int direction) {
        if (direction == 0) {
            showVertical(message, R.mipmap.icon_notify_done);
        } else {
            showHorizontal(message, R.mipmap.icon_notify_done);
        }
    }

    public static void error(String message) {
        error(message, 0);
    }

    public static void error(String message, int direction) {
        if (direction == 0) {
            showVertical(message, R.mipmap.icon_notify_error);
        } else {
            showHorizontal(message, R.mipmap.icon_notify_error);
        }
    }

    public static void info(String message) {
        info(message, 1);
    }

    public static void info(String message, int direction) {
        if (direction == 0) {
            showVertical(message, R.mipmap.icon_notify_info);
        } else {
            showHorizontal(message, R.mipmap.icon_notify_info);
        }
    }

    public static void normal(String message) {
        showHorizontal(message, 0);
    }

    private static void showVertical(String message, int resId) {
        Context context = Config.getApplicationContext();

        View view = LayoutInflater.from(context).inflate(R.layout.toast_custom_vertical_layout, null);
        ImageView imageView = view.findViewById(R.id.iv);
        imageView.setImageResource(resId);
        TextView textView = view.findViewById(R.id.tv_text);
        textView.setText(message);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 280);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    private static void showHorizontal(String message, int resId) {
        Context context = Config.getApplicationContext();

        View view = LayoutInflater.from(context).inflate(R.layout.toast_custom_horizontal_layout, null);
        ImageView imageView = view.findViewById(R.id.iv);
        if (resId == 0) {
            imageView.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(resId);
        }

        TextView textView = view.findViewById(R.id.tv_text);
        textView.setText(message);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 280);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

}
